package com.jyt.utils;

import java.io.*;

/**
 * @Description: JAVA调用算法的工具类
 * @Author hugh.sun
 * @Date 2021/11/24 11:57
 */

/*线程类  当调用这个线程的时候读数据 看一下输出结果*/
class DealProcessStream extends Thread {
    InputStream inputStream;

    public DealProcessStream(InputStream is) {
        this.inputStream = is;
    }

    public void run() {

        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(inputStream);
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.print(line);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                br.close();
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/*调python*/
public class JavaCallPython{


    /*读取python写出的数据 在workpath这个路径下面的一个result.txt"中*/
    public static String readTxtFile() {

        try {
            File file = new File(workpath + "result.txt");
            if (file.isFile() && file.exists()) {
                System.out.println(workpath + "result.txt");
                InputStreamReader read = new InputStreamReader(new FileInputStream(file));
                BufferedReader br = new BufferedReader(read);
                String line = null;
                String last = null;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                    last = line;
                }
                read.close();
                return last;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    static String workpath = "E:\\testpicture";
    //这个名字是当时的时间年月日时分秒
        public static String CallPython(String ImageName) {

        Process proc;
        String result = "";
        try {
            /*
             * 总而言之就是传一个图片名字 返回以一个文本.txt
             * */


			/*调用python需要传三个参数 ：
			1.系统安装python的路径
			2.要调用的Python脚本文件
			3.要传递给Python的脚本URL（图片的地址）
			4.通过数据传递
			5.启动线程执行
			6.最终返回 会写到你 写的static String workpath = "/www/caffe/models/peleenet/";这个文件下一个叫

			*
			*/
            String python = "python";//系统安装的python路径，可以通过命令 which python 查看
            String pyfile = "testhjw.py";// 要调用的Python脚本文件
            String urlStr = "test_file/" + ImageName;//要传递给Python脚本的 URL
            //String cmdStr = String.format("%s %s", pyfile, urlStr);


            String[] cmds = new String[] { python, pyfile, urlStr };
            /*执行python脚本   new File这个参数是 返回的txt从放在这个文件夹下面*/

            proc = Runtime.getRuntime().exec(cmds, null, new File(workpath));// 执行py文件

            System.out.println("等待命令结果...");
            /*看一下输出结果*/
            new DealProcessStream(proc.getInputStream()).start();

            proc.waitFor();

            result = readTxtFile();
            System.out.println("RESULT:" + result);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*返回结果在txt文本中查询的结果*/
        return result;
    }
}
